var data_set = [];
var relative_coord = [];
var graphic_height = 390;
var graphic_width = 1000;
var graphic_object;
var points_per_window = 20;
var temp_start_point;
var start_point = 900;
var mouse_x = -1;
var space = -1;

function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

function FillData() {
	for (var i = 0; i < 1000; i++)
		data_set[data_set.length] = getRandomInt(1000, 9999);
}

function RelativeCoord() {
	for (var i = 0; i < 1000; i++)
		relative_coord[i] = graphic_height - 
			(data_set[i] - 1000)*graphic_height/8999;
}

/* function GRoll() {
	start_point--;
	BuildGraphic();
} */

function getMouseXY(e) {
	var temp_x;
	if (mouse_x == -1) mouse_x = e.pageX;
	temp_x = e.pageX;
	start_point = temp_start_point - Math.floor((temp_x - mouse_x)/space);
	BuildGraphic();
}

function StopRolling() {
	document.onmousemove = "null";
	document.onmouseup = "null";
}

function GRoll() {
	temp_start_point = start_point;
	document.onmousemove = getMouseXY;
	document.onmouseup = StopRolling;
}

function BuildGraphic() {
	var d_attribute = "M";
	
	d_attribute += "0," + relative_coord[start_point];
	
	var end_point = start_point + points_per_window;
	space = graphic_width/points_per_window;
	
	for (var i = start_point + 1; i <= end_point; i++) {
		d_attribute += " L" + (i - start_point)*space + "," +
			relative_coord[i];
	}
	graphic_object.setAttribute("d", d_attribute);
	if (start_point == 0) return;
	//start_point--;
	//window.setTimeout("BuildGraphic()", 10);
}

function Init() {
	FillData();
	RelativeCoord();
	graphic_object = document.getElementById("graphic");
	BuildGraphic();
}